package com.lc.user.express;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.user.express.app.AppContext;
import com.lc.user.express.app.AppManager;


/**
 * @author Administrator
 * @ClassName:BaseSecondActivity 基类antivity 标题栏   继承后获得标题栏样式    左：返回  中：标题 右：文字或图片按钮
 */
public class BaseSecondActivity extends FragmentActivity implements View.OnClickListener {

    public Context cnt;
    private TextView head_title;//title
    private RelativeLayout head_layout;//标题栏布局
    public TextView head_left, head_right;// title左按钮 右侧按钮
    private LinearLayout head_left_ll;//左侧按钮容器，扩大点击范围


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        cnt = this;
        // 解决手机系统设置中字体超大后，某些软件界面错乱问题
        Resources res = getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

    /**
     * 初始化标题栏
     */
    private void loadTitleView() {
        head_layout = (RelativeLayout) findViewById(R.id.head_layout);
        head_title = (TextView) findViewById(R.id.head_title);
        head_left = (TextView) findViewById(R.id.head_left);
        head_left_ll = (LinearLayout) findViewById(R.id.head_left_ll);
        head_right = (TextView) findViewById(R.id.head_right);
        head_left_ll.setOnClickListener(this);
        head_right.setOnClickListener(this);

    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.base_title);
        loadTitleView();
        LinearLayout llContent = (LinearLayout) findViewById(R.id.llContent);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(layoutResID, llContent, false);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) v);
        llContent.addView(v);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) getWindow().getDecorView());
    }

    /**
     * activity跳转
     *
     * @param cls                      类名
     * @param flags                    添加flag
     * @param isStartActivityForResult true or false
     * @param requestCode              requestCode
     * @param bundle                   bundle传值
     */
    public void show(Class<?> cls, int flags, boolean isStartActivityForResult, int requestCode, Bundle bundle) {
        Intent i = new Intent(cnt, cls);
        if (flags != -1) {
            i.addFlags(flags);
        }

        if (null != bundle) {
            i.putExtras(bundle);
        }

        if (isStartActivityForResult) {
            startActivityForResult(i, requestCode);
        } else {
            startActivity(i);
        }

    }

    public void show(Class<?> cls, int flags, boolean isStartActivityForResult, int requestCode) {
        show(cls, flags, isStartActivityForResult, requestCode, null);
    }

    public void show(Class<?> cls, int flags) {
        show(cls, flags, false, -1);
    }

    public void show(Class<?> cls) {
        show(cls, -1, false, -1);
    }

    public void show(Class<?> cls, Bundle bundle) {
        show(cls, -1, false, -1, bundle);
    }

    public void show(Class<?> cls, int flags, Bundle bundle) {
        show(cls, flags, false, -1, bundle);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        head_title.setText(title);
    }

    /**
     * 设置标题Title的颜色
     *
     * @param colorId
     */
    public void setTitleTextBackgroundColor(int colorId) {
        head_title.setTextColor(colorId);
    }


    /**
     * 修改左上角按钮背景图片
     *
     * @param resid
     */
    public void setLeftButtonBgResId1(int resid) {
        head_left.setBackgroundResource(resid);
        head_left.setVisibility(View.VISIBLE);
        head_left_ll.setVisibility(View.VISIBLE);
    }

    /**
     * @param resid
     * @param h
     * @param w     左上角按钮 指定大小
     */
    public void setLeftButtonBgResId2(int resid, int h, int w) {
        head_left.setBackgroundResource(resid);
        head_left.setVisibility(View.VISIBLE);
        head_left_ll.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams pa = (LinearLayout.LayoutParams) head_left.getLayoutParams();
        pa.width = AppContext.ScaleScreenHelper.getWidthHeight(w);
        pa.height = AppContext.ScaleScreenHelper.getWidthHeight(h);
        head_left.setLayoutParams(pa);
    }

    /**
     * 设置左上角按钮是否显示
     *
     * @param visibility
     */
    public void setLeftButtonVisibility(int visibility) {
        head_left.setVisibility(visibility);
        head_left_ll.setVisibility(View.VISIBLE);
    }

    /**
     * @param visibility 设置右上角按钮是否显示 默认隐藏
     */
    public void setRightButtonVisibility(int visibility) {
        head_right.setVisibility(visibility);
    }

    /**
     * @param text
     * @param w    右侧按钮 指定 宽度 针对文字按钮
     */
    public void setRightButtonText(CharSequence text, int w) {
        head_right.setVisibility(View.VISIBLE);
        head_right.setText(text);
        head_right.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);

        RelativeLayout.LayoutParams pa = (RelativeLayout.LayoutParams) head_right.getLayoutParams();
        pa.width = AppContext.ScaleScreenHelper.getWidthHeight(w);
        head_right.setLayoutParams(pa);

    }


    /**
     * @param visibility
     * @return void
     * @throws
     * @Title: setHeadLayoutVisibility
     * @Description: TODO 设置是否显示标题栏
     */
    public void setHeadLayoutVisibility(int visibility) {
        head_layout.setVisibility(visibility);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.head_left_ll) {//左侧按钮点击
            goBack(v);
        }
    }

    public void goBack(View v) {
        onBackPressed();
    }

    /**
     * TODO 子类重写此方法，可同时监听左上角返回键和手机物理返回键
     *
     * @see Activity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onPause() {
        
        /* TODO Auto-generated method stub*/
        super.onPause();
//        JPushInterface.onPause(cnt);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //每次返回界面时，将点击标志设置为可点击
//        JPushInterface.onResume(cnt);
    }
}

